﻿Imports System.Data.OleDb
Public Class EditProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim sql As String = "SELECT * FROM UserProfile WHERE DinerId=@f8"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("SheetsConnectionString").ConnectionString)
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

            cmd.Parameters.AddWithValue("@f8", User.Identity.Name)

            Dim dr = cmd.ExecuteReader()

            dr.Read()

            If Not IsDBNull(dr("FirstName")) Then tb_FirstName.Text = dr("FirstName")
            If Not IsDBNull(dr("LastName")) Then tb_LastName.Text = dr("LastName")
            If Not IsDBNull(dr("Gender")) Then tb_Gender.Text = dr("Gender")
            If Not IsDBNull(dr("age")) Then tb_Age.Text = dr("age")

            If Not IsDBNull(dr("Grades")) Then tb_TeachingGrade.Text = dr("Grades")
            If Not IsDBNull(dr("TeachingSubject")) Then tb_TeachingSubject.Text = dr("TeachingSubject")

            If Not IsDBNull(dr("DateOfBirth")) Then
                Dim userDoB As DateTime = dr("DateOfBirth")
                ddl_Year.SelectedValue = userDoB.Year
                ddl_Month.SelectedValue = userDoB.Month
                ddl_Day.SelectedValue = userDoB.Day
            End If

            If Not IsDBNull(dr("portrait")) Then
                txtimg.Text = dr("portrait")
                imgpicture.ImageUrl = dr("portrait")
            End If


        End If
        'end data

    End Sub

    Protected Sub btn_Submit_Click(sender As Object, e As EventArgs) Handles btn_Submit.Click

        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString() & getsuffix(fup_Picture.FileName)
        If fup_Picture.HasFile Then
            fup_Picture.SaveAs(Server.MapPath("~/Images") & "/" & newFileName)
            txtimg.Text = "~/Images/" & newFileName
            imgpicture.ImageUrl = txtimg.Text

        End If

        Dim userDoB As DateTime = New DateTime(CInt(ddl_Year.SelectedValue), CInt(ddl_Month.SelectedValue), CInt(ddl_Day.SelectedValue))
        ' Dim dateofbirth As String

        '  dateofbirth = userDoB.Date.ToString("MM/dd/yyyy")


        Dim sql As String = "UPDATE UserProfile SET FirstName=@f1, LastName=@f2, Portrait=@f3,  DateOfBirth=@f4,Grades=@f5, TeachingSubject=@f6, Gender=@f7 ,age=@f8 WHERE DinerId=@f9"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("SheetsConnectionString").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", tb_FirstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_LastName.Text)
        cmd.Parameters.AddWithValue("@f3", txtimg.Text)
        cmd.Parameters.AddWithValue("@f4", userDoB.Date.ToString("MM/dd/yyyy"))
        cmd.Parameters.AddWithValue("@f5", tb_TeachingGrade.Text)
        cmd.Parameters.AddWithValue("@f6", tb_TeachingSubject.Text)
        cmd.Parameters.AddWithValue("@f7", tb_Gender.Text)
        cmd.Parameters.AddWithValue("@f8", tb_Age.Text)
        cmd.Parameters.AddWithValue("@f9", User.Identity.Name)


        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()

        '  MsgBox(" data has been updated")


    End Sub
    Function getsuffix(ByRef fn As String)
        Dim fileParts = fn.Split(".")
        Dim Suffix = fileParts(fileParts.Length - 1)
        Return "." & Suffix
    End Function

    Protected Sub ddl_Day_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddl_Day.SelectedIndexChanged

    End Sub
End Class